/*
 * BAT_Factory.h
 *
 * Factory for all plugin classes in BAT.
 *
 * $Id: BAT_Factory.c 924 2006-09-03 01:19:46Z rpz $
 *
 * Bob Zimmermann <rpz@cse.wustl.edu> Totally free.
 *
 */

#include "BAT_Factory.h"
#include "BAT_GTF_Parser.h"
#include "BAT_GeneID_Validator.h"
#include "BAT_GTF_Writer.h"

BAT_Factory *g_fac = NULL;

void
_BAT_Factory_init_all()
{
  g_fac->parsers = keyed_set_new(1, NULL);
  g_fac->validators = keyed_set_new(1, NULL);
  g_fac->writers = keyed_set_new(1, NULL);

  /* parsers */
#define _PARSE_ADD(c) keyed_set_add(g_fac->parsers,(void*)&(c),(c).type)
  _PARSE_ADD(BAT_GTF_Parse_Plugin);
#undef  _PARSE_ADD

  /* validators */
#define _VAL_ADD(c) keyed_set_add(g_fac->validators,(void*)&(c),(c).type)
  _VAL_ADD(BAT_GeneID_Validate_Plugin);
#undef  _VAL_ADD

  /* writers */
#define _WRITE_ADD(c) keyed_set_add(g_fac->writers,(void*)&(c),(c).type)
  _WRITE_ADD(BAT_GTF_Write_Plugin);
#undef  _WRITE_ADD
}

BAT_Factory *
BAT_Factory_new()
{
  if (g_fac == NULL) {
    g_fac = malloc(sizeof(BAT_Factory));
    g_fac->refcount = 0;
    _BAT_Factory_init_all();
  }
  g_fac->refcount++;
  return g_fac;
}

void
BAT_Factory_delete(BAT_Factory * f, int befrei)
{
  /* this is a singleton class, we ignore the 'befrei' param */
  g_fac->refcount--;
  if (g_fac != NULL && g_fac->refcount == 0) {
    keyed_set_delete(g_fac->parsers, 1);
    keyed_set_delete(g_fac->validators, 1);
    keyed_set_delete(g_fac->writers, 1);
    free(g_fac);
    g_fac = NULL;
  }
}

BAT_Parse_Plugin *
BAT_Factory_get_parse_plugin(BAT_Factory * f, BAT_Parser_Type t)
{
  return keyed_set_get(g_fac->parsers, t);
}

BAT_Validate_Plugin *
BAT_Factory_get_validate_plugin(BAT_Factory * f, BAT_Validator_Type t)
{
  return keyed_set_get(g_fac->validators, t);
}

BAT_Write_Plugin *
BAT_Factory_get_write_plugin(BAT_Factory * f, BAT_Writer_Type t)
{
  return keyed_set_get(g_fac->writers, t);
}

darray_t *
BAT_Factory_get_parse_plugins(BAT_Factory * f)
{
  return keyed_set_get_darray(g_fac->parsers);
}

darray_t *
BAT_Factory_get_validate_plugins(BAT_Factory * f)
{
  return keyed_set_get_darray(g_fac->validators);
}

darray_t *
BAT_Factory_get_write_plugins(BAT_Factory * f)
{
  return keyed_set_get_darray(g_fac->writers);
}
